package tv_model;

import java.io.IOException;
import java.net.InetAddress;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.jmdns.JmDNS;
import javax.jmdns.ServiceInfo;

public class JmDNSService {
	
    private JmDNS jmdns;
    private ServiceInfo serviceInfos;
  
    
    public JmDNSService(Tv tv) {
        System.out.println("Starting JmDns");

        try {
            jmdns = JmDNS.create(InetAddress.getByName(HostAddressParser.parserAddress("eth0"))); 
        } catch (IOException ex) {
            Logger.getLogger(JmDNSService.class.getName()).log(Level.SEVERE, null, ex);
        }

        serviceInfos = ServiceInfo.create(Constants.SERVICE_TYPE, tv.getId(), Constants.PORT, "tvService"); 

        try {
            jmdns.registerService(serviceInfos);
            System.out.println("JmDNS working..."); //TODO afficher sur Emulator_Tv
        } catch (IOException ex) {
            Logger.getLogger(JmDNSService.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
    
    public void close() {
        System.out.println("Stoping JmDns"); //TODO afficher sur Emulator_Tv
        jmdns.unregisterService(serviceInfos);
        jmdns.unregisterAllServices();
        try {
            jmdns.close();
        } catch (IOException ex) {
            Logger.getLogger(JmDNSService.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    
	

}
